package com.sijibao.amap;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.google.gson.Gson;
import com.yicai.sijibao.utl.ClientInfo;

/**
 * 定位服务
 * @author JiangLiangJun
 *
 */
public class LocationBindService extends Service implements AMapLocationListener {
	
	private LocationManagerProxy mAMapLocManager = null;
	
	public  String locationJson;
	public  AMapLocation location;
	
	public boolean gps ;
	
	
	
	public   class  LocationBiner extends Binder{
		
		public String getLocation(){
			if(locationJson!=null){
				return locationJson;
			}
			
			return null;
		}
		public AMapLocation getAMapLocation(){
			if(location!=null){
				return location;
			}
			
			return null;
		}
		
	}
	
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getStringExtra("gps")!=null){
			gps = true ;
		}
		startLocation();
		return new LocationBiner();
	}
	
	
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		stopLocation();
		super.onDestroy();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		stopLocation();
		return super.onUnbind(intent);
	}
	
	
	
	
	private void startLocation(){
		mAMapLocManager = LocationManagerProxy.getInstance(this);
		
		if(gps)
			mAMapLocManager.setGpsEnable(true);
		else
			mAMapLocManager.setGpsEnable(false);
		/*
		 * mAMapLocManager.setGpsEnable(false);//
		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * ，第一个参数是定位provider，第二个参数时间最短是5000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
		 */
//		mAMapLocManager.requestLocationUpdates(
//				LocationProviderProxy.AMapNetwork, 5000, 100, this);
		mAMapLocManager.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 1000, this);
	
		
		
	}


	
	/**
	 * 销毁定位
	 */
	@SuppressWarnings("deprecation")
	private void stopLocation() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
	}
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onLocationChanged(AMapLocation location) {
		
		if(location!=null){
			Gson gson = new Gson();
			MyAmapLocation myLocation = MyAmapLocation.amapLocationToMy(location);
			this.location = location;
			locationJson = gson.toJson(myLocation);		
			
			ClientInfo info=ClientInfo.build(getBaseContext());
			info.latitude=(long) (myLocation.latitude*1e6);
			info.longitude=(long) (myLocation.longitude*1e6);
			
			MyAmapLocation myLocation1 = MyAmapLocation.amapLocationToMy(location);	
			LocationService.staticLocationJson = gson.toJson(myLocation1);		
			//LocationService.staticLocationJson = locationJson;

		}
		
	}
	
	
	
	

}
